package com.pronhub.cloud.shop.service.impl;

import com.pronhub.cloud.shop.domain.OrderInfo;
import com.pronhub.cloud.shop.mapper.OrderInfoMapper;
import com.pronhub.cloud.shop.mapper.SeckillGoodMapper;
import com.pronhub.cloud.shop.msg.SeckillCodeMsg;
import com.pronhub.cloud.shop.service.IOrderInfoService;
import com.pronhub.cloud.shop.service.ISeckillGoodService;
import com.pronhub.cloud.shop.service.ISeckillOrderService;
import com.pronhub.cloud.shop.util.IdGenerateUtil;
import com.pronhub.cloud.shop.web.vo.SeckillGoodVo;
import com.pronhub.shop.cloud.BussinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
public class OrderInfoServiceImpl implements IOrderInfoService {

    //库存-1用到的服务
    @Autowired
    private ISeckillGoodService seckillGoodService;
    //往数据库里面添加详细的订单数据
    @Autowired
    private OrderInfoMapper orderInfoMapper;
    //中间表
    @Autowired
    private ISeckillOrderService seckillOrderService;


    @Override
    @Transactional//需要使用事务包装操作原子性  一次执行成功才算成功
    public String insertOrder(Long userId, Long seckillId) {
        //业务逻辑
        // 1 库存-1
        int count = seckillGoodService.decrSeckillCountBySeckillId(seckillId);
        if (count==0){
            //没有库存了 就不需要创建订单
            throw new BussinessException(SeckillCodeMsg.ALREADY_SELL_OUt);
        }
        // 2 返回一个订单号 这个订单号码用到推特的雪花计算 因为要保密数据
        String orderNo = this.creatOrederAndInsert(userId,seckillId);
        // 3 往数据库里面添加订单号
        seckillOrderService.createSeckillOrder(userId,seckillId,orderNo);//创建秒杀订单t_seckill_order
        return orderNo;
    }

    @Override
    public OrderInfo find(String orderNo) {
        return orderInfoMapper.find(orderNo);
    }

    @Override
    public void cancelOrder(Long seckillId, String orderNo) {
        //取消订单
        //1 查询订单的真实状态 根据订单号去查询订单
        OrderInfo orderInfo = orderInfoMapper.find(orderNo);
        //2支付了 不做任任何处理
        //3没有支付 数据库相对应的回滚
        if (orderInfo.getStatus().equals(OrderInfo.STATUS_ARREARAGE)){
            //     3.1 设置订单状态为 超时取消订单
            int count = orderInfoMapper.updateCancelStatus(orderNo,OrderInfo.STATUS_TIMEOUT);
            if (count==0){
                //在修改订单状态前，已经有其他请求修改了订单的状态.
                throw new  BussinessException(SeckillCodeMsg.CANCEL_ORDER_ERROR);
            }
            //     3.2 真实库存+1
            seckillGoodService.incrStock(seckillId);
            //     3.3 取消本地标识
            //     3.4 设置预库存的数量
            seckillGoodService.syncRedisStockCount(seckillId);
        }
    }

    @Override
    public int changePayStatus(String orderNo, Integer status) {
        return orderInfoMapper.changePayStatus(orderNo,status);
    }


    private String creatOrederAndInsert(Long userId, Long seckillId){
        //新建一个订单 然后一个一个的填入数据
        OrderInfo orderInfo = new OrderInfo();
        SeckillGoodVo seckillGoodVo = seckillGoodService.findById(seckillId);
        orderInfo.setCreateDate(new Date());//订单创建时间
        orderInfo.setDeliveryAddrId(null);//收货地址id
        orderInfo.setGoodCount(1);//购买的商品数量
        orderInfo.setGoodId(seckillGoodVo.getGoodId());//商品id
        orderInfo.setGoodImg(seckillGoodVo.getGoodImg());//商品图片
        orderInfo.setGoodName(seckillGoodVo.getGoodName());//商品名称
        orderInfo.setGoodPrice(seckillGoodVo.getGoodPrice());//商品原价
        orderInfo.setSeckillPrice(seckillGoodVo.getSeckillPrice());//商品秒杀价格
        orderInfo.setUserId(userId);//用户

        //订单号码需要用到一个雪花算法
        orderInfo.setOrderNo(IdGenerateUtil.get().nextId()+"");//订单编号，使用推特雪花算法生成订单号
        orderInfoMapper.insert(orderInfo);
        //返回的是订单号
        return orderInfo.getOrderNo();
    }
}
